using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GameStateManagement
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Asteroid : Actor
    {
        private static float m_fSpeed = 500.0f;
        private static float m_fRotSpeed = 0.05f;

        Random rand = new Random();

        public int m_LastCollisionTime = 0;

        Force asteroidForce;

        public Asteroid(Game game)
            : base(game)
        {
            sMeshName = "Asteroid";

            fMass = 0.1f;
            fTerminalVelocity = 200.0f;
            vAcceleration = Vector3.Zero;
            bPhysicsDriven = true;

            asteroidForce = CreateForce("General Force Vector");
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // Set velocity to random direction
            asteroidForce.vec.X = rand.Next(-500, 500);
            asteroidForce.vec.Y = rand.Next(-500, 500);
            asteroidForce.vec.Z = rand.Next(-500, 500);

            // Set to the const speed
            asteroidForce.vec.Normalize();
            asteroidForce.vec *= m_fSpeed;

            // Initial rotation
            m_qRotation = Quaternion.CreateFromAxisAngle(Vector3.Right, (float)rand.NextDouble() * MathHelper.TwoPi) * Quaternion.CreateFromAxisAngle(Vector3.Up, (float)rand.NextDouble() * MathHelper.TwoPi) * Quaternion.CreateFromAxisAngle(Vector3.Backward, (float)rand.NextDouble() * MathHelper.TwoPi);

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // Rotation
            m_qRotation *= Quaternion.CreateFromAxisAngle(WorldTransform.Up, MathHelper.ToRadians(m_fRotSpeed)) * Quaternion.CreateFromAxisAngle(WorldTransform.Right, MathHelper.ToRadians(m_fRotSpeed)) * Quaternion.CreateFromAxisAngle(WorldTransform.Backward, MathHelper.ToRadians(m_fRotSpeed));

            base.Update(gameTime);
        }
    }
}
